package com.itheima.info.manager.dao;

import com.itheima.info.manager.domain.Person;
import com.itheima.info.manager.domain.Student;

import java.util.ArrayList;

public class StudentDao implements BaseStudentDao {

    private static ArrayList<Student> studentArrayList = new ArrayList<>();
    static {
        Student student = new Student("0001","王五",23,"1991-1-1");
        Student student2 = new Student("0002","赵六",28,"1993-1-1");
        Student student3 = new Student("0003","李四",21,"1994-1-1");
        studentArrayList.add(student);
        studentArrayList.add(student2);
        studentArrayList.add(student3);
    }
    //添加学生
    @Override
    public void addStudent(Student student) {
        studentArrayList.add(student);
    }
    //删除学生
    @Override
    public void deleteStudent(String id) {
        int i = getIndex(id);
        studentArrayList.remove(i);
    }
    //查看学生
    @Override
    public ArrayList findAllStudent() {
        return studentArrayList;
    }
    //修改学生
    @Override
    public void updateStudent(String id, Student student) {
        int i = getIndex(id);
        studentArrayList.set(i,student);
    }
    //根据id查索引
    @Override
    public int getIndex(String id) {
        Student student = new Student();
        int index = -1;
        for (int i = 0; i < studentArrayList.size(); i++) {
            student = studentArrayList.get(i);
            if (student != null && student.getId().equals(id)){
                index = i;
            }
        }
        return index;
    }
}
